//
//  YHConstant+Null.m
//  YHConstant
//
//  Created by 98du on 2021/11/22.
//

#import "YHConstant+Null.h"


BOOL IsNull(id obj){
    if(!obj){
        return YES;
    }
    if(obj == nil || [obj isEqual:[NSNull class]] || [obj isKindOfClass:[NSNull class]]){
        return YES;
    }
    if([obj isKindOfClass:[NSString class]]){
        NSString * str = (NSString *)obj;
        if([str isEqualToString:@""]){
            return YES;
        }
        if ([[str stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]){
            return YES;
        }
    }
    
    return NO;
}
BOOL IsNonNull(id obj){
    return !IsNull(obj);
}

BOOL IsEmpty(id obj){
    if(IsNull(obj)){
        return YES;
    }
    if([obj isKindOfClass:[NSArray class]]){
        NSArray * list = (NSArray *)obj;
        BOOL isNull = NO;
        for (NSObject * object in list) {
            if ([object isKindOfClass:NSString.class]) {
                BOOL classType = IsNull(object);
                if (!isNull && classType) {
                    isNull = YES;
                }
                
            }else if ([object isKindOfClass:NSNull.class]) {
                if (!isNull) {
                    isNull = YES;
                }
            }
        }
        if (!isNull) {
            return list.count == 0;
        }else{
            return isNull;
        }
    }
    if([obj isKindOfClass:[NSDictionary class]]){
        NSDictionary * dic = (NSDictionary *)obj;
        return dic.count == 0;
    }
    if([obj isKindOfClass:[NSString class]]){
        NSString * str = (NSString *)obj;
        return str.length == 0;
    }
    if([obj isKindOfClass:[NSSet class]]){
        NSSet * set = (NSSet *)obj;
        return set.count == 0;
    }
    if([obj isKindOfClass:[NSData class]]){
        NSData * data = (NSData *)obj;
        return data.length == 0;
    }
    return NO;
}
BOOL IsNotEmpty(id obj){
    return !IsEmpty(obj);
}


/// 如果是空的显示 @"" 空的字符串
NSString * NullShow(id obj){
    if(IsNull(obj)){
        return @"";
    }
    if(![obj isKindOfClass:[NSString class]]){
        return [NSString stringWithFormat:@"%@",obj];
    }
    return obj;
}
/// 如果是空的显示 - 替代
NSString * NullShowReplaceLine(id obj){
    if(IsNull(obj)){
        return @"-";
    }
    if(![obj isKindOfClass:[NSString class]]){
        return [NSString stringWithFormat:@"%@",obj];
    }
    return obj;
}


@implementation YHConstant (Null)

@end
